/**
* @version v 0.2.0 2009/10/19
* @package Nerv Widgets, nw.js
* @subpackage Nerv Table, window.js
* @author Manuele Castellan <manuele.castellan@gmail.com>
*
* @copyright    Copyright (C) Copyright (c) 2009 Manuele Castellan. All rights reserved.
* @license        GNU/GPLv3, see LICENSE
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

NW3.table = {
	// create TABLE tag for table
	Table : function() {
		var oTable = document.createElement("table");
		var oTbody = document.createElement("tbody");
		
		/* CREO OGGETTO begin */
		var obj = {
			object : oTbody,		// elemento HTML
			otable : oTable,
			/* attributi */
			border : 0,
			cellpadding : 0,
			cellspacing : 0,
			align : null,
			width : null,
			height : null,
			
			classprefix : 'NWTable'
		}
		/* CREO OGGETTO end */
		
		obj.display = function(val){
			if(val == true){
				obj.otable.style.display = 'block';
			}
			else if(val == false){
				obj.otable.style.display = 'none';
			}
		}
		
		obj.setBorder = function(val){
			if(val != ''){
				obj.border = val;
				obj.otable.border = val;
			}
		}
		
		obj.setWidth = function(val){
			if(val){
				obj.width = val;
			}
			obj.otable.style.width = obj.width;
		}
		
		obj.setHeight = function(val){
			if(val){
				obj.height = val;
			}
			obj.otable.style.height = obj.height;
		}
		
		obj.setCellPadding = function(val){
			if(val != ''){
				obj.cellpadding = val;
			}
			obj.otable.cellPadding = obj.cellpadding;
		}
		
		obj.setCellSpacing = function(val){
			if(val){
				obj.cellspacing = val;
			}
			obj.otable.cellSpacing = obj.cellspacing;
		}
		
		obj.setAlign = function(val){
			if(val != ''){
				obj.align = val;
				obj.otable.align = val;
			}
		}
		
		obj.setClass = function(val){
			if(val){
				obj.classprefix = val;
			}
			obj.otable.className = obj.classprefix;
		}
		
		// crea tabella da un array multimensionale
		obj.importFromArray = function(arr, arrHead){
			if(arr.length > 0){
				// table header
				if(arrHead.length > 0){
					var oTr = new NW3.table.Tr();
					var oTr_ = oTr.output();
					//var oTr = document.createElement("tr");
					for (h=0; h<arrHead.length; h++)
					{
						var oTh = new NW3.table.Th();
						var oTh_ = oTh.output();
						
						//var oTh = document.createElement("th");
						var oText = document.createTextNode(arrHead[h]);
						oTh_.appendChild(oText);
						oTr_.appendChild(oTh_);
					}
					obj.object.appendChild(oTr_);
				}

				// table
				for (x=0; x<arr.length; x++)
				{
					var oTr = new NW3.table.Tr();
					var oTr_ = oTr.output();
					
					//var oTr = document.createElement("tr");
					for (y=0; y<arr[x].length; y++)
				    {
				    	var oTd = new NW3.table.Td();
						var oTd_ = oTd.output();
				    	
				    	//var oTd = document.createElement("td");
						var oText = document.createTextNode(arr[x][y]);
						oTd_.appendChild(oText);
						oTr_.appendChild(oTd_);
				    }
					obj.object.appendChild(oTr_);
				}
			}
		}
		
		// appendo oggetto
		obj.add = function(oObj){
			obj.object.appendChild(oObj.output());
		}
		
		
		// output oggetto
		obj.output = function(){
			obj.setWidth();
			obj.setHeight();
			obj.otable.border = obj.border;
			obj.otable.cellSpacing = obj.cellspacing;
			obj.otable.cellPadding = obj.cellpadding;
			
			/*obj.setCellPadding();
			obj.setCellSpacing();*/
			
			obj.setClass();
			
			obj.otable.appendChild(obj.object);
			
			return obj.otable;
		}
		
		obj.replace = function(targetElement){
			NW3.core.object.replace(targetElement, obj.output());
		};
		
		obj.insertAfter = function(targetElement){
			NW3.core.object.insertAfter(obj.output(), targetElement);
		};
		
		obj.insertBefore = function(targetElement){
			NW3.core.object.insertBefore(obj.output(), targetElement);
		};
		
		obj.appendChild = function(targetElement){
			NW3.core.object.appendChild(obj.output(), targetElement);
		};
		
		return obj;
	},
	
	// create TR tag for table
	Tr : function() {
		var oTr = document.createElement("tr");
		
		/* CREO OGGETTO begin */
		var obj = {
			object : oTr,                            // elemento HTML
			stopEvent : false,
			
			classprefix : null
		}
		/* CREO OGGETTO end */
		
		// appendo oggetto
		obj.add = function(oObj){
			obj.object.appendChild(oObj.output());
		}
		
		/** setta id */
		obj.setId = function(node, id){
			node.id = id;
		}
		
		/** rimuovi id */
		obj.removeId = function(node){
			node.removeAttribute('id');
		}
		
		/** recupera id */
		obj.getId = function(node){
			return node.id;
		}
		
		obj.setClass = function(val){
			if(val != ''){
				obj.classprefix = val;
				obj.object.className = val;
			}
		}
		
		// output oggetto
		obj.output = function(){
			return obj.object;
		}
		
		return obj;
	},
	
	// create TD tag for table
	Td : function() {
		oTd = document.createElement("td");
		
		/* CREO OGGETTO begin */
		var obj = {
			object : oTd,			// elemento HTML
			colspan : null,
			align : null,
			
			onclick : null,
			
			classprefix : null
		}
		/* CREO OGGETTO end */
		
		// eventi
		//var oTdEvent = new NW3.event.Handling(oTd);
		// setto onClick
		NW3.event.addEvent(oTd, "click", function(ev){
			// eseguo funzione utente
			if(obj.onclick) obj.onclick(obj);
		});
		
		// setto onclick
		obj.setOnClick = function(val){
			if(val){
				obj.onclick = val;
			}
		}
		
		// appendo oggetto
		obj.add = function(oObj){
			obj.object.appendChild(oObj);
		}
		
		obj.setClass = function(val){
			if(val != ''){
				obj.classprefix = val;
				obj.object.className = val;
			}
		}
		
		obj.setAlign = function(val){
			if(val != ''){
				obj.align = val;
				obj.object.align = val;
			}
		}
		
		obj.setColSpan = function(val){
			if(val != ''){
				obj.colspan = val;
				obj.object.colSpan = val;
			}
		}
		
		// output oggetto
		obj.output = function(){
			return obj.object;
		}
		
		return obj;
	},
	
	// create TH tag for table
	Th : function() {
		oTh = document.createElement("th");
		
		/* CREO OGGETTO begin */
		var obj = {
			object : oTh,
			align : null,
			classprefix : null                         // elemento HTML
		}
		/* CREO OGGETTO end */
		
		// appendo oggetto
		obj.add = function(oObj){
			obj.object.appendChild(oObj);
		}
		
		obj.setClass = function(val){
			if(val != ''){
				obj.classprefix = val;
				obj.object.className = val;
			}
		}
		
		obj.setAlign = function(val){
			if(val != ''){
				obj.align = val;
				obj.object.align = val;
			}
		}
		
		// output oggetto
		obj.output = function(){
			return obj.object;
		}
		
		return obj;
	}
}
